home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / GetScreenDevice.c < prev    next >
Text File  |  1995-10-14  |  8KB  |  239 lines

  1. /* GetScreenDevice.c
  2.  
  3.     device=GetScreenDevice(n);
  4. returns a handle to the n-th screen, where the MainDevice is the zero-th screen. 
  5. This is similar, but not identical, to the screen number in the Monitors
  6. Control panel.
  7.  
  8.     n=GetScreenIndex(device);
  9. returns the inverse of GetScreenDevice(). 
  10.  
  11.     slot=GetDeviceSlot(device);
  12. gets the "slot" for any screen device. 
  13.  
  14.     device=SlotToScreenDevice(n);
  15. returns a handle to the screen device in slot n. 
  16.  
  17.     device=GetWindowDevice(window);
  18. returns GDHandle of screen with largest intersection with the window's content.
  19.  
  20.     GDHandle    GetDeviceByRefNum(int n);
  21.     GDHandle    GetRectDevice(Rect *r);
  22.  
  23.     void        LocalToGlobalRect(Rect *r);
  24.     void        GlobalToLocalRect(Rect *r);
  25.  
  26. Copyright © 1989-1995 Denis G. Pelli
  27. HISTORY:
  28. 3/20/90        dgp    make compatible with MPW C.
  29. 3/22/90    dgp    changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
  30.             from the baseAddr of the PixMap. This is a cleaner way to do it.
  31. 4/9/90    dgp    eliminated #define for Mainscrn mispelling in Color.h
  32. 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
  33.             the Mac IIci, IIsi, and LC.
  34. 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
  35. 8/24/91        dgp    Made compatible with THINK C 5.0.
  36. 2/1/92    dgp    Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
  37. 3/3/92    dgp    In GetScreenDevice(), skip inactive screens.
  38. 8/20/92    dgp    expanded comments of GetDeviceSlot(), noting that it works even with
  39.             built-in video, e.g. on Mac IIci.
  40. 8/26/92    dgp    GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
  41.             GetScreenDevice() first checks for 8-bit QuickDraw().
  42. 9/10/92    dgp    Actually implemented the 8/26 change instead of just changing the 
  43.             documentation. Oops.
  44. 4/17/93    dgp Deleted obsolete AddressToSlot and AddressToScreenDevice.
  45. 5/21/93    dgp    Fixed GetWindowDevice() to support GWorld's.
  46. 8/14/93    dgp    Based on answer from DEVSUPPORT, I cleaned up GetWindowDevice().
  47. 4/11/94    dgp    Added GetRectDevice() based on code extracted from GetWindowDevice().
  48. 11/30/94 dgp added GetDeviceByRefNum().
  49. 6/1/95 dgp moved LocalToGlobalRect() and GlobalToLocalRect() to CenterRectInRect.c
  50. 10/4/95 dgp GetDeviceSlot() nows checks for presence of Slot Manager and returns -1 if it's absent.
  51. 10/14/95 dgp added GetDeviceSlotName(), which works on all Macs, including PCI.
  52. */
  53. #include "VideoToolbox.h"
  54. void GetVideoProperties(GDHandle device,char *slotName,char *cardName,char *modelName);
  55. char *GetDeviceSlotName(GDHandle device);
  56.  
  57. GDHandle GetScreenDevice(int n)
  58. // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
  59. // Returns NULL if request can't be satisfied.
  60. {
  61.     GDHandle device;
  62.     int i,error;
  63.     long value;
  64.  
  65.     if(n<0)return NULL;
  66.     error=Gestalt(gestaltQuickdrawVersion,&value);
  67.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  68.     if(n==0) return GetMainDevice();
  69.     device=GetDeviceList();
  70.     i=0;
  71.     while(device!=NULL){
  72.         if (TestDeviceAttribute(device,screenDevice)
  73.             && !TestDeviceAttribute(device,mainScreen)
  74.             && TestDeviceAttribute(device,screenActive)){
  75.                 i++;
  76.                 if(i==n)break;
  77.             }
  78.         device = GetNextDevice(device);
  79.     }
  80.     return device;
  81. }
  82.  
  83. GDHandle GetDeviceByRefNum(int n)
  84. {
  85.     int j;
  86.     GDHandle device;
  87.  
  88.     for(j=0;;j++){
  89.         device=GetScreenDevice(j);
  90.         if(device==NULL || n==(**device).gdRefNum)break;
  91.     }
  92.     return device;
  93. }
  94.  
  95. int GetScreenIndex(GDHandle device)
  96. // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
  97. {
  98.     int i,error;
  99.     long value;
  100.  
  101.     error=Gestalt(gestaltQuickdrawVersion,&value);
  102.     if(error || value<gestalt8BitQD)return 0;
  103.     if(device==NULL)return -1;
  104.     for(i=0;i<16;i++)if(device==GetScreenDevice(i))return i;
  105.     return -1;
  106. }
  107.  
  108. short int GetDeviceSlot(GDHandle device)
  109. // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
  110. // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
  111. // Zero is a legal slot for built-in video devices.
  112. // WARNING: gives nonsense answer on PCI Macs. Use GetDeviceSlotName() instead.
  113. {
  114.     AuxDCEHandle myAuxDCEHandle;
  115.     SpBlock spBlock;
  116.     Boolean slotMgrPresent;
  117.     
  118.     if(TrapAvailable(_SlotManager))slotMgrPresent=(SVersion(&spBlock)==noErr);
  119.     else slotMgrPresent=0;
  120.     if(!slotMgrPresent || device==NULL || (*device)->gdRefNum==0)return -1;
  121.  
  122.     myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
  123.     return ((**myAuxDCEHandle).dCtlSlot);
  124. }
  125.  
  126. char *GetDeviceSlotName(GDHandle device)
  127. // Gets the "slot" name for any screen device, even built-in video, on Macs with NuBus, 
  128. // PCI bus, and no bus. Returns "" if none.
  129. // Zero is a legal slot for built-in video devices.
  130. {
  131.     static char slotName[32]="";
  132.     char cardName[32],modelName[32];
  133.     
  134.     if(device==NULL || (*device)->gdRefNum==0)return "";
  135.     GetVideoProperties(device,slotName,cardName,modelName);
  136.     if(strlen(cardName)==0)sprintf(slotName,"%d",(int)GetDeviceSlot(device));
  137.     return slotName;
  138. }
  139.  
  140. GDHandle SlotToScreenDevice(int n)
  141. // Returns a handle to the screen device in slot n.
  142. // Returns NULL if request can't be satisfied.
  143. {
  144.     GDHandle device;
  145.  
  146.     device=GetDeviceList();
  147.     while(device!=NULL) {
  148.         if (TestDeviceAttribute(device,screenDevice) &&
  149.             GetDeviceSlot(device)==n)
  150.                 break;
  151.         device=GetNextDevice(device);
  152.     }
  153.     return device;
  154. }
  155.  
  156. GDHandle GetWindowDevice(WindowPtr window)
  157. // For on-screen window, returns GDHandle of screen with largest intersection with the 
  158. // window's content.
  159. // For off-screen window (i.e. GWorld), it returns the GDHandle of the associated device.
  160. {
  161.     Rect r;
  162.     WindowPtr oldWindow;
  163.     long qD;
  164.  
  165.     if(window==NULL)return NULL;
  166.     Gestalt(gestaltQuickdrawVersion,&qD);
  167.     if(qD>=gestalt32BitQD && (((CWindowPtr)window)->portVersion&0xc001)==0xc001){
  168.         // It's a GWorld iff the portVersion has both high bits set (cGrafPort) and
  169.         // the low bit set (GWorld). See Apple Tech Note “RowBytes Revealed II”.
  170.         return GetGWorldDevice((GWorldPtr)window);
  171.     }
  172.     r=window->portRect;
  173.     GetPort(&oldWindow);
  174.     SetPort(window);
  175.     LocalToGlobalRect(&r);
  176.     SetPort(oldWindow);
  177.     return GetRectDevice(&r);
  178. }
  179.  
  180. GDHandle GetRectDevice(Rect *r)
  181. // Returns GDHandle of screen with largest intersection with the global rect.
  182. {
  183.     Rect overlap;
  184.     GDHandle device,dominantDevice=NULL;
  185.     long area,greatestArea;
  186.     long qD;
  187.  
  188.     if(r==NULL)return NULL;
  189.     Gestalt(gestaltQuickdrawVersion,&qD);
  190.     if(qD<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  191.     device=GetDeviceList();
  192.     greatestArea=0;
  193.     while(device!=NULL){
  194.         if(TestDeviceAttribute(device,screenDevice)
  195.             && TestDeviceAttribute(device,screenActive)){
  196.                 SectRect(r,&(*device)->gdRect,&overlap);
  197.                 area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
  198.                 if(area>greatestArea){
  199.                     greatestArea=area;
  200.                     dominantDevice=device;
  201.                 }
  202.             }
  203.         device=GetNextDevice(device);
  204.     }
  205.     return dominantDevice;
  206. }
  207.  
  208. #if 0
  209.     #if UNIVERSAL_HEADERS
  210.         #ifndef __DISPLAYS__
  211.             #include <Displays.h>
  212.         #endif
  213.     #else
  214.         enum{kDMDriverNotDisplayMgrAwareErr = -6228};
  215.         extern pascal OSErr DMSetDisplayMode(GDHandle theDevice,unsigned long mode
  216.             ,unsigned long *depthMode,unsigned long reserved,Handle displayState)
  217.              ={0x303C,0x0A11,0xABEB};
  218.         extern pascal OSErr DMCheckDisplayMode(GDHandle theDevice,unsigned long mode
  219.             ,unsigned long depthMode,unsigned long *switchFlags,unsigned long reserved
  220.             ,Boolean *modeOk)={0x303C,0x0C12,0xABEB};
  221.         enum {
  222.             gestaltDisplayMgrAttr        = 'dply',    /* Display Manager attributes */
  223.             gestaltDisplayMgrPresent    = 0            /* True if Display Mgr is present */
  224.         };
  225.     #endif
  226.     
  227.     /* 
  228.     These enum definitions for use with the cscGetNextResolution driver status call 
  229.     are taken from the A7 draft (Feb 10,'95) of Apple's Designing PCI Cards and Drivers for
  230.     Power Mac Computers. Undoubtedly these enum definitions will appear in some future 
  231.     version of Video.h, perhaps with CodeWarrior 6, at which time they can be deleted from here.
  232.     */
  233.     enum{
  234.         kDisplayModeIDCurrent=0
  235.         ,kDisplayModeIDInvalid=0xffffffff
  236.         ,kDisplayModeIDFindFirstResolution=0xfffffffe
  237.         ,kDisplayModeIDNoMoreResolutions=0xfffffffd
  238.     };
  239. #endif